function ActionButton(opts) {
  this.frameName = opts.frameName;
  this.frameKey = opts.frameKey || 0;
  this.x = opts.x;
  this.y = opts.y;
  this.delegate = opts.delegate;
  this.canClick = true;
  this._attackType = opts.attackType;
  this._timestamp = 0;
  this.freezeTime = opts.freezeTime || 600;


  this.beforeAnimation = function() {};
  this.afterAnimation = function() {};
}

ActionButton.prototype.init = function() {

  this.sprite = game.add.sprite(this.x, this.y, this.frameName, this.frameKey);
  this.sprite.anchor.set(0.5);

  this.sprite.inputEnabled = true;
  this.sprite.events.onInputDown.add(this.doAttack, this);
  this.sprite.events.onInputUp.add(this.completeAttack, this);
};

ActionButton.prototype.doAttack = function() {
  // if(this.delegate && this.canClick){
  //   this.delegate.attackButtonClick(this.getAttackType());
    this.beforeAnimation();      
  // }
  var timestamp = (new Date()).valueOf();   
  this._timestamp = timestamp;
};

ActionButton.prototype.completeAttack = function() {
  this.afterAnimation();
};

ActionButton.prototype.getAttackType = function() {
  return this.attackType;
};


ActionButton.prototype.canClick = function(callback) {
  var timestamp = (new Date()).valueOf();   
  return timestamp - this._timestamp > this.freezeTime;
};

ActionButton.prototype.setAfterAnimation = function(callback) {
  this.afterAnimation = callback;
};

ActionButton.prototype.setBeforeAnimation = function(callback) {
  this.beforeAnimation = callback;
};

function AttackButton(){
  this.maxScale = 0.4;
  this.defaultScale = 0.35;
  this.pt = null;

  this.button = new ActionButton({
    frameName: 's_Attack',
    x: game.world.width - 80,
    y: game.world.height - 80,
    attackType: 'attact'
  });

  
  this.button.setBeforeAnimation(function() {
    if (this.pt) {
      this.pt.destroy();
    }
    this.pt = game.add.sprite(game.world.width - 80, game.world.height - 80, 's_AttackO');
    this.pt.alpha = 0;
    this.pt.scale.setTo(0.1, 0.1);

    this.pt.anchor.set(0.5);
    var tween = game.add.tween(this.pt.scale).to({x: this.defaultScale, y: this.defaultScale}, 100, Phaser.Easing.Linear.None);
    tween.onComplete.addOnce(fadeIn, this);
    tween.start();

    function fadeIn() {
      game.add.tween(this.pt).to( { alpha: 1 }, 100, Phaser.Easing.Linear.None).start();
    }
    
    
  }.bind(this));

  this.button.setAfterAnimation(function() {
    if (!this.pt) {
      return ;
    }
    var tween = game.add.tween(this.pt.scale).to({x: this.maxScale, y: this.maxScale}, 200, Phaser.Easing.Linear.None);
    tween.onComplete.addOnce(fadeOut, this);
    tween.start();

    function fadeOut() {
      game.add.tween(this.pt).to( { alpha: 0 }, 200, Phaser.Easing.Linear.None).start();
    }
  }.bind(this));


  this.button.init();
};